﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using VetumaForSharePoint.Resources;
using VetumaForSharePoint.DAL;

namespace VetumaForSharePoint
{
    public class VFSAdMembershipProvider : ActiveDirectoryMembershipProvider
    {
        // These are security question and answer required by active directory membership provider.
        // Because user is already authenticated with Vetuma, no real need to ask these from user.
        private const string securityQuestion = "Are you sure?";
        private const string securityQuestionAnswer = "Yes";

        /// <summary>
        /// Create AD user account.
        /// </summary>
        /// <param name="username">The user name for new user.</param>
        /// <param name="password">The password for new user.</param>
        /// <param name="email">The email of the new user.</param>
        /// <param name="socialSecurityNumber">The social security number of the new user.</param>
        /// <returns>MembershipUser object.</returns>
        public MembershipUser CreateUser(string username, string password, string email, string socialSecurityNumber)
        {
            MembershipCreateStatus creationStatus;

            // Use base class to create user.
            MembershipUser user = base.CreateUser(username, password, email, securityQuestion, securityQuestionAnswer, true, null, out creationStatus);

            // If account was successfully created, return user object else throw exception or 
            if (creationStatus == MembershipCreateStatus.Success)
            {
                return user;
            }
            else if (creationStatus == MembershipCreateStatus.DuplicateUserName)
            {
                throw new VFSException(VetumaForSP.ResourceManager.GetString("UserNameExists"));
            }
            else if (creationStatus == MembershipCreateStatus.InvalidPassword)
            {
                throw new VFSException(VetumaForSP.ResourceManager.GetString("InvalidPassword"));
            }
            else if (creationStatus == MembershipCreateStatus.DuplicateEmail)
            {
                throw new VFSException(VetumaForSP.ResourceManager.GetString("EmailExists"));
            }
            else if (creationStatus == MembershipCreateStatus.InvalidEmail)
            {
                throw new VFSException(VetumaForSP.ResourceManager.GetString("InvalidEmail"));
            }

            return null;
        }

    }
}
